/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Ad;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.ProductImage;
import com.hboxs.asl.service.AdPositionService;
import com.hboxs.asl.service.AdService;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Controller - 广告
 */
@Controller("adminAdController")
@RequestMapping("/asl-admin/ad")
public class AdController extends BaseController {

    @Resource(name = "adServiceImpl")
    private AdService adService;
    @Resource(name = "adPositionServiceImpl")
    private AdPositionService adPositionService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        model.addAttribute("adPositions", adPositionService.findAll());
        model.addAttribute("teachers", memberService.findAll());
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.ad);
        return "/admin/ad/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Ad ad, Long adPositionId, RedirectAttributes redirectAttributes) {
        ad.setAdPosition(adPositionService.find(adPositionId));
        if (ad.getBeginDate() != null && ad.getEndDate() != null && ad.getBeginDate().after(ad.getEndDate())) {
            return ERROR_VIEW;
        }
        adService.save(ad);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        Ad ad = adService.find(id);
        model.addAttribute("ad", ad);
        model.addAttribute("teachers", memberService.findAll());
        model.addAttribute("adPositions", adPositionService.findAll());
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.ad);
        return "/admin/ad/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Ad ad, Long adPositionId, RedirectAttributes redirectAttributes) {
        ad.setAdPosition(adPositionService.find(adPositionId));
        if (!isValid(ad)) {
            return ERROR_VIEW;
        }
        if (ad.getBeginDate() != null && ad.getEndDate() != null && ad.getBeginDate().after(ad.getEndDate())) {
            return ERROR_VIEW;
        }
        adService.update(ad);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", adService.findPage(pageable));
        return "/admin/ad/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        adService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}